<?php

if (!defined('_CAN_LOAD_FILES_'))
    exit;

class Blockscheduler extends Module {

    public function __construct() {
        $this->name = 'blockscheduler';
        $this->tab = 'Scheduler';
        $this->tabClassName = 'AdminScheduler';
        $this->tabParentName = 'AdminCustomers';
        $this->need_instance = 0;
        parent::__construct();
        $this->displayName = $this->l('Scheduler soczewki.net');
        $this->description = $this->l('Mechanizm powiadamiania klientów o terminie zakupu soczewek');
        $this->confirmUninstall = $this->l('Are you sure you want to delete all schedulers?');
        $this->version = '1.0';
        $this->author = 'CreativeCoder.pl';
        $this->error = false;
        $this->valid = false;
    }

    public function install() {
        if (!parent::install() || !$this->registerHook('leftColumn') ||
                !$this->registerHook('header'))
            return false;
        return Db::getInstance()->Execute('
		CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'scheduler (
			`id_scheduler` int(6) NOT NULL AUTO_INCREMENT,
			`email` varchar(255) NOT NULL,
			`scheduled_at` DATETIME NULL,
                        `is_send` TINYINT(1) DEFAULT 0,
			PRIMARY KEY(`id_scheduler`)
		) ENGINE=' . _MYSQL_ENGINE_ . ' default CHARSET=utf8');
    }

    public function uninstall() {
        if (!parent::uninstall())
            return false;
        return Db::getInstance()->Execute('DROP TABLE ' . _DB_PREFIX_ . 'scheduler');
    }

    function hookLeftColumn($params) {
        global $smarty;

        if (Tools::isSubmit('submitScheduler')) {
            if (empty($_POST['scheduler_email']) OR !Validate::isEmail($_POST['scheduler_email'])) {
                $error = $this->l('Invalid e-mail address') . '<br />';
            }
            if (empty($_POST['scheduled_at'])) {
                $error .= $this->l('Please enter date') . '<br />';
            }

            if (!empty($error)) {
                $smarty->assign('error', $error);
            } else {
                $result = Db::getInstance()->Execute('INSERT INTO ' . _DB_PREFIX_ . 'scheduler (`email`, `scheduled_at`) 
                        VALUES ("' . strip_tags($_POST['scheduler_email']) . '", "' . strip_tags($_POST['scheduled_at']) . '")');
                if (!$result) {
                    $smarty->assign('error', 'Przepraszamy! Wystąpił problem z zapisem daty przypomnienia.');
                } else {
                    $smarty->assign('success', 'Dziękujemy! W dniu '.strip_tags($_POST['scheduled_at']).' otrzymasz przypomnienie o konieczności uzupełnienia zapasu soczewek.');
                }
            }
        }

        $smarty->assign('this_path', $this->_path);
        $smarty->assign('rememberDay', strtotime('+1 week'));
        return $this->display(__FILE__, 'blockscheduler.tpl');
    }

    public function hookHeader() {
        Tools::addCSS('/css/jquery-ui-1.8.10.custom.css', 'all');
        Tools::addJS('/js/jquery/datepicker/ui/i18n/ui.datepicker-pl.js');
    }

}

